<template>
  <view class="content">
    <view class="text-area">
      <text class="title">{{ title }}</text>
    </view>
	<text>身份证:{{ id }}</text>
	<text>姓名:{{ name }}</text>
	<text>性别:{{ sex }}</text>
	<text>有效时间:{{ validityTime }}</text>
	<image :src="photo"></image>
	<button @click="openTest()">打开</button>
	<button @click="closeTest()">关闭</button>
	
	<button @click="insert()">插入</button>
	<button @click="search()">查询</button>
	<text>查询结果:{{ result }}</text>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
const title = ref('Hello')

let id = ref('')
let name = ref('')
let sex = ref('')
let validityTime = ref('')
let photo = ref('')

// #ifdef APP


import * as idcard from '@/uni_modules/gdty-idcard'
import {IDCardInfoResult} from '@/uni_modules/gdty-idcard'


let timerID = 0;

function openTest(){

	idcard.initIdcard2();
	idcard.open2();
		timeout = false
	time();
	
}

function closeTest(){
	timeout = true
	clearTimeout(timerID)
	idcard.destroy2();
}

let isfind:boolean = true;//是否可读卡
var timeout = false; //启动及关闭按钮  
function time(){  
	if(timeout) return;  
	let resaa:string = idcard.readCardUID();
	console.log(resaa)
	if(!resaa||resaa===''){
		isfind = true;
	}else{
		if(isfind){
			console.log('----------------------读卡')
			isfind = false;
			
			id.value = ''
			name.value = ''
			sex.value = ''
			validityTime.value = ''
			
			try{
				let retType:IDCardInfoResult = idcard.getInfo2(2);
				console.log(retType)
				id.value = retType.id
				name.value = retType.name
				sex.value = retType.sex
				validityTime.value = retType.validityTime
				
				if(retType.photolength>0){
					let img = retType.photoBase.replace(/(\r\n|\n|\r)/gm,'')
					photo.value = "data:image/jpg;base64," + img
				}
			}catch(e){
				
			}
			
				
			
		}
	}
	timerID = setTimeout(time,800); //time是指本身,延时递归调用自己,100为间隔调用时间,单位毫秒  
}  

//初始化
import {init,UniUpdateVo,UniSQLVo,execSQL,queryStr} from '@/uni_modules/gdty-sqliteUtis'
let result = ref('')
let u1:UniUpdateVo = {
	version:1,
	sql:'create table test3(id integer primary key autoincrement,name varchar(64));'
}
let u:UniUpdateVo = {
	version:2,
	sql:'create table test2(id integer primary key autoincrement,name varchar(64));'
}
let vo:UniSQLVo = {
	initSql:'create table test(id integer primary key autoincrement,name varchar(64));',
	updateSql:[u1,u]
}
init('uni.db',2,vo);

function insert(){
	execSQL('insert into test(name) values(\'tim\')')
}

function search(){
	let res:string = queryStr("test","",new Array<string>(),"")
	console.log(res)
	result.value = res
}

// #endif
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 200rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}
</style>
